package com.minimalist.basic.controller.basic;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.minimalist.basic.service.DictService;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.basic.vo.dict.DictCacheVO;
import com.minimalist.common.entity.basic.vo.dict.DictInfoVO;
import com.minimalist.common.entity.basic.vo.dict.DictQueryVO;
import com.minimalist.common.entity.basic.vo.dict.DictVO;
import com.minimalist.common.response.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 11987
 */
@Validated
@RestController
@Tag(name = "字典管理")
@RequestMapping("/basic/dict")
public class DictController{

    @Autowired
    private DictService dictService;

    @PostMapping("/addDict")
    @SaCheckPermission("basic:dict:add")
    @Operation(summary = "添加字典")
    public R<?> addDict(@RequestBody @Validated DictInfoVO dictInfoVO){
        dictService.addDict(dictInfoVO);
        return R.ok();
    }

    @DeleteMapping("/deleteDictByDictId")
    @SaCheckPermission("basic:dict:delete")
    @Operation(summary = "删除字典 -> 根据字典ID删除")
    public R<?> deleteDictByDictId(
            @RequestParam("dictId") @NotNull(message = "字典ID不能为空")
            @Parameter(name = "dictId", required = true, description = "字典ID")
            Long dictId){
        dictService.deleteDictByDictId(dictId);
        return R.ok();
    }

    @DeleteMapping("/deleteDictByDictType")
    @SaCheckPermission("basic:dict:delete")
    @Operation(summary = "删除字典 -> 根据字典类型删除")
    public R<?> deleteDictByDictType(
            @RequestParam("dictType") @NotNull(message = "字典类型不能为空")
            @Parameter(name = "dictType", required = true, description = "字典类型")
            String dictType){
        dictService.deleteDictByDictType(dictType);
        return R.ok();
    }

    @PutMapping("/updateDictByDictId")
    @SaCheckPermission("basic:dict:update")
    @Operation(summary = "修改字典")
    public R<?> updateDictByDictId(@RequestBody @Validated DictInfoVO dictInfoVO){
        dictService.updateDictByDictId(dictInfoVO);
        return R.ok();
    }

    @GetMapping("/getPageDictList")
    @SaCheckPermission("basic:dict:get")
    @Operation(summary = "查询字典列表(分页)")
    public R<PageResp<DictVO>> getPageDictList(DictQueryVO queryVO){
        return R.ok(dictService.getPageDictList(queryVO));
    }

    @GetMapping("/getDictByDictType/{dictType}")
    @SaCheckPermission("basic:dict:get")
    @Operation(summary = "根据字典类型查询字典 -> 用于字典管理页面")
    public R<DictInfoVO> getDictByDictType(@PathVariable(value = "dictType")
                                           @Parameter(name = "dictType", description = "字典类型", required = true) String dictType){
        return R.ok(dictService.getDictByDictType(dictType));
    }

    @GetMapping("/getDictList/{dictTypes}")
    @Operation(summary = "根据字典类型查询字典 -> 用于下拉框数据展示或编码转换")
    public R<List<DictCacheVO>> getDictList(
            @PathVariable(value = "dictTypes")
            @NotEmpty(message = "字典类型不能为空")
            @Parameter(name = "dictTypes", description = "字典类型列表，为空则查询所有字典数据")
            List<String> dictType){
        return R.ok(dictService.getDictList(dictType));
    }

}
